package cn.aezo.minions.gen.mapper;

import cn.aezo.minions.gen.model.ModuleFunction;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by smalle on 2017/10/28.
 */
public interface ModuleFunctionDao {
    @Select("select * from module_function where project_module_id = #{projectModuleId}")
    List<ModuleFunction> findByProjectModuleId(Long projectModuleId);

    @Select("select mf.* from module_function mf join project_module pm on mf.project_module_id = pm.project_module_id where pm.project_config_id = #{projectConfigId}")
    List<ModuleFunction> findByProjectConfigId(Long projectConfigId);

    @Insert("insert into module_function(project_module_id, function_name, function_name_cn, control_mapping_uri, control_method, return_object) " +
            "values(#{projectModuleId}, #{functionName}, #{functionNameCn}, #{controlMappingUri}, #{controlMethod}, #{returnObject})")
    int insert(ModuleFunction moduleFunction);

    @Delete("delete from module_function where project_module_id = #{projectModuleId}")
    void deleteByProjectModuleId(Long projectModuleId);

    // h2如何联表删除？
    // @Delete("delete mf from module_function mf join project_module pm on mf.project_module_id = pm.project_module_id where pm.project_config_id = #{projectConfigId}")
    @Delete("DELETE from module_function where exists (select 1 from project_module pm where project_module_id = pm.project_module_id and pm.project_config_id = #{projectConfigId})")
    void deleteByProjectConfigId(Long projectConfigId);
}
